package com.example.firstapplication;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentProviderOperation.Builder;
import android.content.ContentResolver;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ContactInfoActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact);

		Button btn_save = (Button) this.findViewById(R.id.Save);
		btn_save.setOnClickListener(new save());
		Button btn_search = (Button) this.findViewById(R.id.Button);
		btn_search.setOnClickListener(new search());
		
		
		ListView listView1=(ListView)this.findViewById(R.id.listView1);
		
	}

	private class save implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			EditText numText = (EditText)findViewById(R.id.edit_PhoneNumber);
			EditText nameText = (EditText)findViewById(R.id.name);
			String phone = numText.getText().toString();
			String name=nameText.getText().toString();
			//Uri uri = Uri.parse("content://");
			ContentResolver resolver= getApplicationContext().getContentResolver();
			
			ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
			
			Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
			builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "yyy");
			builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "XXX");
			ops.add(builder.build());
			
			ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
	                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
	                .withValue(ContactsContract.Data.MIMETYPE,
	                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
	                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
	                .build());
	        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
	                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
	                .withValue(ContactsContract.Data.MIMETYPE,
	                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
	                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
	                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2)
	                .build());
	        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
	                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
	                .withValue(ContactsContract.Data.MIMETYPE,
	                        ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
	                .withValue(ContactsContract.CommonDataKinds.Email.DATA, "75706809@qq.com")
	                .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 2)
	                .build());
	        
	        try {
				resolver.applyBatch(ContactsContract.AUTHORITY, ops);
				Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				Toast.makeText(getApplicationContext(), "failue", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			} catch (OperationApplicationException e) {
				// TODO Auto-generated catch block
				Toast.makeText(getApplicationContext(), "failue", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}

		}
	}
	
	
	private class search implements View.OnClickListener {
		private final static String Tag= "search_contact";
		@Override
		public void onClick(View v) {
			EditText numText = (EditText)findViewById(R.id.edit_PhoneNumber);
			String phone = numText.getText().toString();
			ContentResolver resolver= getApplicationContext().getContentResolver();
			Uri uri = Uri.withAppendedPath(ContactsContract.Data.CONTENT_URI, "phones/filter/"+phone);
			Cursor cursor = resolver.query(uri, new String[]{"display_name"}, null, null, null);
			if(cursor.moveToFirst()){
				String name = cursor.getString(0);
				EditText nameText = (EditText)findViewById(R.id.name);
				nameText.setText(name);
				Log.i(Tag, "name = "+name);
			}
		
		}
	}

}
